<?php
namespace Plu\Library\Models\Tga\Messageboard;

use Phalcon\Mvc\Model,
    Phalcon\Db\Column;

class Applicants extends Model {

    public $id;
    public $uid;
    public $appkey;
    public $text;
    public $image;
    public $admire;
    public $status;
    public $createtime;
    public $deleteflag;

    public function initialize() {
        $this->setConnectionService('db.tga');
        Model::setup([
            'notNullValidations' => false
        ]);
    }

    public function getSource() {
        return 'messageboard';
    }

    //
    static public function getItemByUid($appkey, $uid) {
        $item = self::findFirst(array(
            "appkey = :appkey: AND uid = :uid:",
            "bind" => array(
                'appkey' => $appkey,
                'uid'    => $uid
            ),
            "bindTypes" => array(
                'appkey' => Column::BIND_PARAM_STR,
                'uid'    => Column::BIND_PARAM_INT
            ),
            'order' => 'createtime DESC',
            "limit" => 1 
        ));

        if ($item == false) {
            return [];
        } else {
            return $item->toArray();
        }
    }


    static public function getList($appkey, $deleteflag = 0, $limit) {
        $item = self::find(array(
            "appkey = :appkey: AND deleteflag = :deleteflag:",
            "bind" => array(
                'appkey' => $appkey,
                'deleteflag' => $deleteflag
            ),
            "bindTypes" => array(
                'appkey' => Column::BIND_PARAM_STR,
                'deleteflag'    => Column::BIND_PARAM_INT
            ),
            'order' => 'admire DESC',
            "limit" => $limit 
        ));

        if ($item == false) {
            return [];
        } else {
            return $item->toArray();
        }
    }


    static public function getListById($appkey, $start = 0, $limit = 30, $deleteflag = 0) {
        $item = self::find(array(
            "appkey = :appkey: AND deleteflag = :deleteflag:",
            "bind" => array(
                'appkey' => $appkey,
                'deleteflag' => $deleteflag,
            ),
            "bindTypes" => array(
                'appkey' => Column::BIND_PARAM_STR,
                'deleteflag'    => Column::BIND_PARAM_INT
            ),
            'order' => 'createtime DESC',
            "limit" => array(
                "offset" => $start,
                "number"  => $limit
            )

        ));

        if ($item == false) {
            return [];
        } else {
            return $item->toArray();
        }
    }


    static public function getIds($appkey, $deleteflag = 0) {
        $item = self::find(array(
            "appkey = :appkey: AND deleteflag = :deleteflag:",
            "columns" => array(
                'id'
                ),
            "bind" => array(
                'appkey' => $appkey,
                'deleteflag' => $deleteflag
            ),
            "bindTypes" => array(
                'appkey' => Column::BIND_PARAM_STR,
                'deleteflag'    => Column::BIND_PARAM_INT
            ),
            'order' => 'createtime DESC',
            "limit" => 500
        ));

        if ($item == false) {
            return [];
        } else {
            return $item->toArray();
        }
    }



    static function num($appkey, $deleteflag = 0){
        $count = self::count(array(
            "appkey = ?0 AND deleteflag = ?1",
            "bind" => array(
                0 => $appkey,
                1 => $deleteflag)
        ));
        return $count;
    }


    static public function updateMesById($id) {
        $item = self::findFirst(array(
            'id = :id:',
            'bind'  =>  array(
                'id'    =>  $id,
            ),
            "bindTypes" => array(
                'id'    => Column::BIND_PARAM_INT
            )
        ));
        if($item) {
            $item->admire = $item->admire + 1; 
            $res = $item->save();
        } else {
            $res = false;
        }

        return $res;
    }


    static public function addItem($appkey, $uid, $text, $image = '') {
        $item = new self;
        $item->uid = $uid;
        $item->appkey = $appkey;
        $item->text = $text;
        $item->image = $image;
        $item->admire = 0;
        $item->status = 1;
        $item->createtime = time();
        $item->deleteflag = 0;

        return $item->save();
    }
}
?>
